<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <div th:replace="./widget/base"></div>
</head>
<body>

<div id="departmentToolbar">
    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/DEPARTMENT/PAGE_ALL/DEPARTMENT_ADD_DIALOG'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-plus" plain="true"
       onclick="departmentAddDialog();">新增</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/DEPARTMENT/PAGE_ALL/DEPARTMENT_EDIT_DIALOG'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-edit" plain="true"
       onclick="departmentEditDialog();">编辑</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/DEPARTMENT/PAGE_ALL/DEPARTMENT_DELETE_ALL'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-close" plain="true"
       onclick="departmentDeleteAll();">删除</a>
</div>
<table id="department"></table>

<script type="text/javascript">

    $(function () {
        var columns = [
            {field: 'checkbox', checkbox: true},
            {field: 'id', title: 'ID'},
            {field: 'name', title: '部门名称'},
            {field: 'remark', title: '备注'},
            {field: 'gmtModified', title: '更新时间'},
            {field: 'gmtCreated', title: '创建时间'}
        ];
        $('#department').treegrid(expressui.grid.create, {
            title: '部门管理',
            url: Url.User.Api.DEPARTMENT + '/list_all_as_tree_grid',
            columns: [columns],
            pagination: false
        });
        // $('#department').treegrid({
        //     title: '部门管理',
        //     url: Url.User.Api.DEPARTMENT + '/list_all_as_tree_grid',
        //     // expandUrl: Url.User.Api.DEPARTMENT + '/list_all_by_parent_id/{id}',
        //     columns: [columns],
        //     pagination: false
        // });
    });

    function departmentAddDialog() {
        var department = $('#department').treegrid(expressui.grid.getCheckedOneNoCheckedOrShowAlert);

        $('#departmentAddDialog').dialog(expressui.dialog.create, {
            title: '新增部门',
            grid: {type: 'treegrid', selector: '#department'},
            href: Url.User.Page.DEPARTMENT + '/add_dialog',
            save: {
                url: Url.User.Api.DEPARTMENT + '/add_one',
                data: department ? {parentId: department.id} : {}, // 非必传参数
                method: 'post'
            },
            buttons: [{
                text: '保存',
                iconCls: 'fa fa-save',
                handler: expressui.dialog.add,
                reload: [{type: 'treegrid', selector: '#department'}],
                success: '新增成功'
            }, {
                text: '关闭',
                iconCls: 'fa fa-close',
                handler: expressui.dialog.close
            }]
        });
    }

    function departmentEditDialog() {
        var one = $('#department').treegrid(expressui.grid.getCheckedOneOrShowAlert);
        if (one) {
            $('#departmentEditDialog').dialog(expressui.dialog.create, {
                title: '编辑 API 分类',
                grid: {type: 'treegrid', selector: '#department'},
                href: Url.User.Page.DEPARTMENT + '/edit_dialog',
                get: {url: Url.User.Api.DEPARTMENT + '/one/{id}', method: 'get'},
                save: {url: Url.User.Api.DEPARTMENT + '/save_one', method: 'post'},
                buttons: [{
                    text: '保存',
                    iconCls: 'fa fa-save',
                    handler: expressui.dialog.save,
                    reload: [{type: 'treegrid', selector: '#department'}],
                    success: '保存成功'
                }, {
                    text: '关闭',
                    iconCls: 'fa fa-close',
                    handler: expressui.dialog.close
                }]
            });
        }
    }

    function departmentDeleteAll() {
        $('#department').treegrid(expressui.grid.postCheckedLessOneOrShowAlert, {
            url: Url.User.Api.DEPARTMENT + '/delete_all',
            method: 'post',
            success: '删除成功',
            reload: [{type: 'treegrid', selector: '#department'}]
        });
    }

</script>
</body>
</html>